package com.itheima.ydd.controller;

import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.Menu;
import com.itheima.ydd.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * 菜单管理
 */
@RestController
@Slf4j
@RequestMapping("sys_menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    /**
     * 菜单修改
     *
     * @param menu
     */
    @PostMapping("update")
    public ResultData update(@RequestBody Menu menu) {
        log.info("menu:{}", menu);
        menuService.updateById(menu);
        return ResultData.ok("", "菜单修改成功");
    }

    /**
     * 菜单删除
     *
     * @param id
     * @return
     */
    @GetMapping("del")
    public ResultData delete(Integer id) {
        log.info("id:{}", id);
        menuService.removeById(id);
        return ResultData.ok("", " 菜单删除成功");
    }

    /**
     * 所有菜单
     *
     * @return
     */
    @GetMapping("allData")
    public ResultData allData() {
        List<Menu> list = menuService.allData();
        return ResultData.ok("", list);
    }

    /**
     * 菜单添加
     *
     * @param menu
     * @return
     */
    @PostMapping("add")
    public ResultData add(@RequestBody Menu menu) {
        log.info("menu:{}", menu);
        menuService.save(menu);
        return ResultData.ok("", "添加成功");
    }

    /**
     * 菜单详情
     *
     * @return
     */
    @GetMapping("rowInfo")
    public ResultData rowInfo(Integer id) {
        log.info("id:{}", id);
        Menu menu1 = menuService.getById(id);
        return ResultData.ok("", menu1);
    }


}
